import { addHospitalValue, getHospitalID, putHospitalValue } from '@/api/hospital/HospitalSet'
import { addHospital } from '@/api/hospital/model/HospitalSetType'
import { Button, Card, Checkbox, Form, Input, message } from 'antd'
import { useForm } from 'antd/lib/form/Form'
import React, { useEffect } from 'react'
import { useNavigate, useParams } from 'react-router-dom'

export default function HospitalAddOrUpdate() {
    let navigate = useNavigate()
    //  获取地址栏ID
    let { id } = useParams()
    let [form] = useForm()
    // 获取input框数据
    let getinput = async () => {
        let obj: addHospital = form.getFieldsValue()
        if (id) {
            await putHospitalValue({ ...obj, id })
            message.success("修改成功")
        } else {
            await addHospitalValue(obj)
            message.success("添加成功")
        }
        navigate(-1)

    }
    //  生命周期初始化数据
    useEffect(() => {
        async function main() {
            let res = await getHospitalID(id as string)
          
            
            form.setFieldsValue(res)
        }
        main()

    }, [])
    return (
        <Card>
            <Form
                onFinish={getinput}
                form={form}

                labelCol={{ span: 2 }}
                wrapperCol={{ span: 22 }}
            >
                <Form.Item
                    label="医院名称"
                    name="hosname"
                    rules={[{ required: true, message: '请输入医院名称!' }]}
                >
                    <Input />
                </Form.Item>

                <Form.Item
                    label="医院编号"
                    name="hoscode"
                    rules={[{ required: true, message: '请输入医院编号!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="api基础路径"
                    name="apiUrl"
                    rules={[{ required: true, message: '请输入api基础路径!' }]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="联系人姓名"
                    name="contactsName"
                    rules={[{ required: true, message: '请输入联系人姓名  !' },
                    { pattern: /^[\u4e00-\u9fa5]{0,}$/, message: "请输入正确的联系人名字" }
                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item
                    label="联系人手机"
                    name="contactsPhone"
                    rules={[{ required: true, message: '请输入联系人手机号!' },
                    { pattern: /^1([358][0-9]|4[456789]|6[67]|7[0135678]|9[1589])\d{8}$/, message: "请输入正确的手机号" }

                    ]}
                >
                    <Input />
                </Form.Item>
                <Form.Item wrapperCol={{ offset: 2, span: 16 }}>
                    <Button type="primary" htmlType="submit">
                        保存
                    </Button>
                    <Button type="default" className='ml' onClick={() => {
                        navigate(-1)
                    }}>
                        返回
                    </Button>
                </Form.Item>
            </Form>


        </Card>
    )
}
